package cc.cw;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

import oracle.to.mysql.utils.SqlUtils;

public class CB20_CW_Compare {

	private static Logger log = Logger.getLogger(" ");
	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			log.error("驱动加载异常：" + e.getMessage());
		}
	}

	public static void main(String[] args) throws SQLException, IOException {
		long time = System.currentTimeMillis();
		Connection connOracle = DriverManager.getConnection(SqlUtils.ORACLE_URL, SqlUtils.ORACLE_USER, SqlUtils.ORACLE_PWD);
		Statement csOracle = connOracle.createStatement();
		ResultSet rs = csOracle.executeQuery("select acb200,aab004,real_money,give_money from cb20_cw");
		Connection connMysql = DriverManager.getConnection(SqlUtils.getMySqlUrl());
		while (rs.next()) {
			String acb200 = rs.getString("acb200");
			String aab004 = rs.getString("aab004").trim();
			BigDecimal real_money = rs.getBigDecimal("real_money");
			BigDecimal give_money = rs.getBigDecimal("give_money");
			int count = getCount(connMysql, aab004);
			if (count == 0) {
				log.error("mysql中不存在此数据，acb200=" + acb200 + ",aab004=" + aab004);
			} else if (count == 1) {
				String sqlMysql = "select acb200,aab004,real_money,give_money from cb20 where aab004='" + aab004 + "'";
				Statement stmtMysql = connMysql.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_UPDATABLE);
				ResultSet rsMysql = stmtMysql.executeQuery(sqlMysql);
				if(rsMysql.next()){
					String acb200Mysql = rsMysql.getString("acb200");
					BigDecimal real_moneyMysql = rsMysql.getBigDecimal("real_money");
					BigDecimal give_moneyMysql = rsMysql.getBigDecimal("give_money");
					if(check(real_money, give_money, real_moneyMysql, give_moneyMysql)){
						log.error("余额相等，oracle：acb200="+acb200+",aab004="+aab004+",real_money="+real_money+",give_money="+give_money
								+"，acb200Mysql="+acb200Mysql+",real_moneyMysql="+real_moneyMysql+",give_moneyMysql="+give_moneyMysql);
					}else{//余额不等
						log.error("余额不等，oracle：acb200="+acb200+",aab004="+aab004+",real_money="+real_money+",give_money="+give_money
								+",acb200Mysql="+acb200Mysql+",real_moneyMysql="+real_moneyMysql+",give_moneyMysql="+give_moneyMysql);
					}
				}
				rsMysql.close();
				stmtMysql.close();
			} else {
				log.warn("mysql中存在" + count + "条数据,acb200=" + acb200 + ",aab004=" + aab004);
			}
		}
		connOracle.close();
		connMysql.close();
		log.info("全部执行完成，耗时:" + (System.currentTimeMillis() - time) + "ms");
	}

	private static int getCount(Connection conn, String aab004) throws SQLException {
		String sql = "select count(1) from cb20 where aab004='" + aab004 + "'";
		Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
		ResultSet rs = stmt.executeQuery(sql);
		int count = 0;
		if (rs.next()) {
			count = rs.getInt(1);
		}
		rs.close();
		stmt.close();
		return count;
	}

	private static boolean check(BigDecimal real_money, BigDecimal give_money, BigDecimal real_moneyMysql, BigDecimal give_moneyMysql) {
		if (real_money == null) {
			real_money = BigDecimal.ZERO;
		}
		if (give_money == null) {
			give_money = BigDecimal.ZERO;
		}
		if (real_moneyMysql == null) {
			real_moneyMysql = BigDecimal.ZERO;
		}
		if (give_moneyMysql == null) {
			give_moneyMysql = BigDecimal.ZERO;
		}
		real_money = real_money.setScale(2, BigDecimal.ROUND_HALF_DOWN);
		give_money = real_money.setScale(2, BigDecimal.ROUND_HALF_DOWN);
		real_moneyMysql = real_moneyMysql.setScale(2, BigDecimal.ROUND_HALF_DOWN);
		give_moneyMysql = give_moneyMysql.setScale(2, BigDecimal.ROUND_HALF_DOWN);
		if (real_money.compareTo(real_moneyMysql) == 0 && give_money.compareTo(give_moneyMysql) == 0) {
			return true;
		}
		return false;
	}
	
}